package com.personal.portal.common.entity;

import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 统一API返回结果
 * 格式: {code: 0, message: "", data: , timestamp: ''}
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Data
public class Result<T> {
    
    private Integer code;          // 业务状态码: 0-成功, -1-失败, 其他-业务错误
    private String message;        // 响应消息
    private T data;                // 响应数据
    private String timestamp;      // 响应时间戳 (格式: yyyy-MM-dd HH:mm:ss)

    @Data
    public static class PageResult<T> {
        private List<T> data;
        private long total;
        private int pageNum;
        private int pageSize;

        public PageResult(List<T> data, long total, int pageNum, int pageSize) {
            this.data = data;
            this.total = total;
            this.pageNum = pageNum;
            this.pageSize = pageSize;
        }
    }

    /**
     * 成功返回
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(0);
        result.setMessage("操作成功");
        result.setTimestamp(DateUtil.now());
        return result;
    }
    
    /**
     * 成功返回带数据
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = success();
        result.setData(data);
        return result;
    }
    
    /**
     * 成功返回带消息和数据
     */
    public static <T> Result<T> success(String message, T data) {
        Result<T> result = success(data);
        result.setMessage(message);
        return result;
    }
    
    /**
     * 失败返回
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(-1);
        result.setMessage("操作失败");
        result.setTimestamp(DateUtil.now());
        return result;
    }
    
    /**
     * 失败返回带消息
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = error();
        result.setMessage(message);
        return result;
    }
    
    /**
     * 失败返回带状态码和消息
     */
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = error(message);
        result.setCode(code);
        return result;
    }
    
    /**
     * 未授权
     */
    public static <T> Result<T> unauthorized() {
        Result<T> result = new Result<>();
        result.setCode(401);
        result.setMessage("未授权访问");
        result.setTimestamp(DateUtil.now());
        return result;
    }
    
    /**
     * 禁止访问
     */
    public static <T> Result<T> forbidden() {
        Result<T> result = new Result<>();
        result.setCode(403);
        result.setMessage("禁止访问");
        result.setTimestamp(DateUtil.now());
        return result;
    }
    
    /**
     * 资源未找到
     */
    public static <T> Result<T> notFound() {
        Result<T> result = new Result<>();
        result.setCode(404);
        result.setMessage("资源未找到");
        result.setTimestamp(DateUtil.now());
        return result;
    }
    
    /**
     * 参数错误
     */
    public static <T> Result<T> badRequest() {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMessage("参数错误");
        result.setTimestamp(DateUtil.now());
        return result;
    }
    
    /**
     * 参数错误带消息
     */
    public static <T> Result<T> badRequest(String message) {
        Result<T> result = badRequest();
        result.setMessage(message);
        return result;
    }
}